package com.web.model;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
public class ScreenShot {

	private File screenshot;
	
	public ScreenShot(File screenshot){
		
		if(screenshot != null){
			
			this.screenshot = screenshot;
		}
		else
			throw new NullPointerException("ScreenShot cannot be null");
	}
	
	public void saveWithDate(String filePath, String name){
		
		this.saveToFile(filePath + name + " - " + String.valueOf(new Date()).replace(":", "-") + ".png");
	}
	
	public void saveToFile(String file){
		
		if(file != null){
			
			File savingFile = new File(file);
			
			tryToCopyFile(screenshot, savingFile);
		}
		else
			throw new NullPointerException("File name cannot be null");
	}
	
	private void tryToCopyFile(File origin, File destiny){
	
		try {
			
			FileUtils.copyFile(origin, destiny);
		
		} catch (IOException e) {

			throw new IllegalStateException(String.format("Problems copying file origin '%s' to file destiny '%s'", origin, destiny), e);
		}
	}
	
	
}
